package com.mall.db.api;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.db.mapper.*;
import com.mall.domain.*;
import com.mall.dto.ProductListDto;
import com.mall.interface1.api.PmsProductApi;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class PmsProductApiImpl implements PmsProductApi {

    @Autowired
    private PmsProductMapper pmsProductMapper;

    @Autowired
    private PmsMemberPriceMapper pmsMemberPriceMapper;

    @Autowired
    private CmsPrefrenceAreaProductRelationMapper cmsPrefrenceAreaProductRelationMapper;

    @Autowired
    private PmsProductAttributeValueMapper pmsProductAttributeValueMapper;

    @Autowired
    private PmsProductFullReductionMapper pmsProductFullReductionMapper;

    @Autowired
    private PmsProductLadderMapper pmsProductLadderMapper;

    @Autowired
    private PmsSkuStockMapper pmsSkuStockMapper;

    @Autowired
    private CmsSubjectProductRelationMapper cmsSubjectProductRelationMapper;

    //保存商品基本信息，并返回商品id
    public Long saveProduct(PmsProduct pmsProduct) {
        pmsProductMapper.insert(pmsProduct);
        return pmsProduct.getId();
    }

    @Override
    public void savePmp(PmsMemberPrice pmp) {
        pmsMemberPriceMapper.insert(pmp);
    }

    @Override
    public void saveCpapr(CmsPrefrenceAreaProductRelation cpapr) {
        cmsPrefrenceAreaProductRelationMapper.insert(cpapr);
    }

    @Override
    public void savePpav(PmsProductAttributeValue ppav) {
        pmsProductAttributeValueMapper.insert(ppav);
    }

    @Override
    public void savePpfr(PmsProductFullReduction ppfr) {
        pmsProductFullReductionMapper.insert(ppfr);
    }

    @Override
    public void savePpl(PmsProductLadder ppl) {
        pmsProductLadderMapper.insert(ppl);
    }

    @Override
    public void savePss(PmsSkuStock pss) {
        pmsSkuStockMapper.insert(pss);
    }

    @Override
    public void saveCspr(CmsSubjectProductRelation cspr) {
        cmsSubjectProductRelationMapper.insert(cspr);
    }

    //分页查询出商品信息
    public List<PmsProduct> list(ProductListDto dto) {
        Page<PmsProduct> pageInfo = new Page<>(dto.getPageNum(),dto.getPageSize());
        LambdaQueryWrapper<PmsProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dto.getBrandId() != null,PmsProduct::getBrandId,dto.getBrandId());
        queryWrapper.like(StrUtil.isNotEmpty(dto.getKeyword()),PmsProduct::getName,dto.getKeyword());
        queryWrapper.eq(dto.getProductCategoryId() != null,PmsProduct::getProductCategoryId,dto.getProductCategoryId());
        queryWrapper.like(StrUtil.isNotEmpty(dto.getProductSn()),PmsProduct::getProductSn,dto.getProductSn());
        queryWrapper.eq(dto.getPublishStatus() != null,PmsProduct::getPublishStatus,dto.getPublishStatus());
        queryWrapper.eq(dto.getVerifyStatus() != null,PmsProduct::getVerifyStatus,dto.getVerifyStatus());
        Page<PmsProduct> pmsProductPage = pmsProductMapper.selectPage(pageInfo, queryWrapper);
        return pmsProductPage.getRecords();
    }

    //查询商品总数
    public Integer count(ProductListDto dto) {
        LambdaQueryWrapper<PmsProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dto.getBrandId() != null,PmsProduct::getBrandId,dto.getBrandId());
        queryWrapper.like(StrUtil.isNotEmpty(dto.getKeyword()),PmsProduct::getName,dto.getKeyword());
        queryWrapper.eq(dto.getProductCategoryId() != null,PmsProduct::getProductCategoryId,dto.getProductCategoryId());
        queryWrapper.like(StrUtil.isNotEmpty(dto.getProductSn()),PmsProduct::getProductSn,dto.getProductSn());
        queryWrapper.eq(dto.getPublishStatus() != null,PmsProduct::getPublishStatus,dto.getPublishStatus());
        queryWrapper.eq(dto.getVerifyStatus() != null,PmsProduct::getVerifyStatus,dto.getVerifyStatus());
        return pmsProductMapper.selectCount(queryWrapper);
    }

    //根据商品名称或货号模糊查询
    public List<PmsProduct> simpleList(String keyword) {
        LambdaQueryWrapper<PmsProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotEmpty(keyword),PmsProduct::getName,keyword)
                .or()
                .eq(StrUtil.isNotEmpty(keyword),PmsProduct::getProductSn,keyword);
        return pmsProductMapper.selectList(queryWrapper);
    }

    //根据id查询商品
    public PmsProduct findById(Long id) {
        return pmsProductMapper.selectById(id);
    }

    //修改商品
    public void update(PmsProduct pmsProduct) {
        pmsProductMapper.updateById(pmsProduct);
    }

    @Override
    public void updatePmp(PmsMemberPrice pmp) {
        pmsMemberPriceMapper.updateById(pmp);
    }

    @Override
    public void updateCpapr(CmsPrefrenceAreaProductRelation cpapr) {
        cmsPrefrenceAreaProductRelationMapper.updateById(cpapr);
    }

    @Override
    public void updatePpav(PmsProductAttributeValue ppav) {
        pmsProductAttributeValueMapper.updateById(ppav);
    }

    @Override
    public void updatePpfr(PmsProductFullReduction ppfr) {
        pmsProductFullReductionMapper.updateById(ppfr);
    }

    @Override
    public void updatePpl(PmsProductLadder ppl) {
        pmsProductLadderMapper.updateById(ppl);
    }

    @Override
    public void updatePss(PmsSkuStock pss) {
        pmsSkuStockMapper.updateById(pss);
    }

    @Override
    public void updateCspr(CmsSubjectProductRelation cspr) {
        cmsSubjectProductRelationMapper.updateById(cspr);
    }

    @Override
    public List<PmsMemberPrice> findPmp(Long id) {
        LambdaQueryWrapper<PmsMemberPrice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(id != null,PmsMemberPrice::getProductId,id);
        return pmsMemberPriceMapper.selectList(queryWrapper);
    }

    @Override
    public List<CmsPrefrenceAreaProductRelation> findPprl(Long id) {
        LambdaQueryWrapper<CmsPrefrenceAreaProductRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(id != null,CmsPrefrenceAreaProductRelation::getProductId,id);
        return cmsPrefrenceAreaProductRelationMapper.selectList(queryWrapper);
    }

    @Override
    public List<PmsProductAttributeValue> findPavl(Long id) {
        LambdaQueryWrapper<PmsProductAttributeValue> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(id != null,PmsProductAttributeValue::getProductId,id);
        return pmsProductAttributeValueMapper.selectList(queryWrapper);
    }

    @Override
    public List<PmsProductFullReduction> findPfrl(Long id) {
        LambdaQueryWrapper<PmsProductFullReduction> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(id != null,PmsProductFullReduction::getProductId,id);
        return pmsProductFullReductionMapper.selectList(queryWrapper);
    }

    @Override
    public List<PmsProductLadder> findPll(Long id) {
        LambdaQueryWrapper<PmsProductLadder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(id != null,PmsProductLadder::getProductId,id);
        return pmsProductLadderMapper.selectList(queryWrapper);
    }

    @Override
    public List<PmsSkuStock> findSsl(Long id) {
        LambdaQueryWrapper<PmsSkuStock> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(id != null,PmsSkuStock::getProductId,id);
        return pmsSkuStockMapper.selectList(queryWrapper);
    }

    @Override
    public List<CmsSubjectProductRelation> findSprl(Long id) {
        LambdaQueryWrapper<CmsSubjectProductRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(id != null,CmsSubjectProductRelation::getProductId,id);
        return cmsSubjectProductRelationMapper.selectList(queryWrapper);
    }
}
